<template>
	<view class="yhrw-zximage-index">
		<view class="content">
			<view class="images" v-for="(item,index) in tempFiles" :key="index">
				<image @click="previewImg(index)" class="img" :src="item.path" mode="aspectFill"></image>
				<view class="remove" @click="removeImg(index)">x</view>
			</view>
			<view class="files" @click="addFiles" v-if="tempFiles.length<9">
				<view class="add">+</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tempFiles: [],
			}
		},
		model: {
		    prop: 'value',
		    event: 'input'
		  },
		onLoad() {

		},
		watch: {
		    value: {
		      handler(val) {
		        
		      },
		      deep: true,
		      immediate: true
		    }
		  },
		methods: {
			addFiles() {
				uni.chooseImage({
					count: 9, // 最大选择的张数，默认是九张，可以不写
					success: res => {
						// console.log(res);
						// 接受选择的图片数组
						let oldImg = [...this.tempFiles, ...res.tempFiles]
						// console.log(oldImg);
						let newImg = oldImg.slice(0, 9)
						// console.log(newImg);
						this.tempFiles = newImg
						this.$emit("input", this.tempFiles);
					}
				})
			},
			// 预览图片
			previewImg(index) {
				// console.log(this.tempFiles);
				// 过滤
				let imgUrl = this.tempFiles.map(item => item.path)
				// console.log(imgUrl);
				uni.previewImage({
					current: index, //点击的索引值
					urls: imgUrl,
				})
			},
			// 删除图片
			removeImg(index) {
				this.tempFiles.splice(index, 1)
				this.$emit("input", this.tempFiles);
			},
			goUpload() {
				// console.log(this.tempFiles, 'this.tempFiles')
				// let promissState = this.tempFiles.map(async item => {
				// 	return await this.uploadFn(item)
				// })
				// // console.log(promissState);
				// Promise.all(promissState).then(res => {
				// 	// console.log(res);
				// 	let imgUrl = res.map(item => item.fileID)
				// 	// console.log(imgUrl);
				// 	const objImg = {
				// 		...imgUrl
				// 	}
				// 	this.uploadDataBase(objImg)
				// }).catch(e => {
				// 	console.log(e);
				// })
			},
			// 上传到云存储
			uploadFn(item) {
				return uniCloud.uploadFile({
					filePath: item.path,
					// cloudPathAsRealPath: true, //让cloudPath作为文件存储路径
					// cloudPath: "images/" + item.name //上传到images文件夹下
					cloudPath: item.name,
					//文件夹模式，不允许文件覆盖
				})
			},
			// 上传到数据库
			uploadDataBase(objImg) {
				uniCloud.callFunction({
					name: 'uploadDataBase',
					data: objImg
				}).then(res => {
					console.log(res);
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.upload {
		width: 120rpx;
		height: 60rpx;
		line-height: 60rpx;
		background-color: #04c062;
		color: white;
		border-radius: 10rpx;
		text-align: center;
		font-size: 30rpx;
	}

	.yhrw-zximage-index {
		.content {
			display: flex;
			flex-wrap: wrap;

			.files {
				width: 200rpx;
				height: 200rpx;
				background-color: #f7f7f7;
				color: #6d6d6d;
				font-size: 100rpx;
				font-weight: 100;
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 10rpx;
			}

			.images {
				width: 200rpx;
				height: 200rpx;
				margin: 10rpx;
				position: relative;

				.img {
					width: 100%;
					height: 100%;
				}

				.remove {
					width: 50rpx;
					height: 50rpx;
					background-color: #f7f7f7;
					position: absolute;
					top: -5rpx;
					right: -10rpx;
					text-align: center;
					opacity: .5;
					border-radius: 0 0 0 50rpx;
					color: #c5c5c5;
				}
			}
		}
	}
</style>